package com.jiadeyu.controller;

import com.jiadeyu.pojo.Result;
import com.jiadeyu.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 上传文件相关
 */
@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
     String originalFileName =  file.getOriginalFilename();
        String fileName = null;
        if (originalFileName != null) {
            fileName = UUID.randomUUID().toString()  + originalFileName.substring(originalFileName.lastIndexOf("."));
        }
        String url = AliOssUtil.uploadFile(fileName, file.getInputStream());
        return Result.success(url);
    }
}
